package com.jinkeen.xiaxie.chatRoom

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.jinkeen.xiaxie.base.BaseActivity
import com.jinkeen.xiaxie.databinding.ActivityViewPageBinding

class ViewPageActivity : BaseActivity() {
    private lateinit var binding: ActivityViewPageBinding
    private var fragments: ArrayList<Fragment>? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityViewPageBinding.inflate(layoutInflater)
        setContentView(binding.root)
        fragments = ArrayList()
        fragments?.add(MeInfoFragment())
        fragments?.add(FriendsFragment())
        fragments?.add(ChatFragment())
        fragments?.add(DiscoverFragment())
        binding.viewpager2.adapter = ViewPageAdapter(this, fragments!!)
        //BottomNavigationView的点击切换fragment
        binding.bottom2.setOnNavigationItemReselectedListener { item ->
            binding.viewpager2.currentItem = item.itemId
        }

    }

    private inner class ViewPageAdapter(
        fa: FragmentActivity,
        private val fragments: ArrayList<Fragment>
    ) : FragmentStateAdapter(fa) {
        override fun getItemCount(): Int = fragments.size

        override fun createFragment(position: Int): Fragment = fragments[position]
    }
}

